﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone;
using System.Threading;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace OmnipotentFlashlight
{
    public partial class LED : PhoneApplicationPage
    {
        private VideoCamera cam = null;
        private bool IsShow = false;

        public LED()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(LED_Loaded);
        }

        //页面加载事件
        void LED_Loaded(object sender, RoutedEventArgs e)
        {
            Microsoft.Phone.Shell.PhoneApplicationService.Current.ApplicationIdleDetectionMode =
                           Microsoft.Phone.Shell.IdleDetectionMode.Disabled;
            if (cam == null)
            {
                StartCam();
            }
        }

        //初始化Cam
        void StartCam()
        {
            cam = new VideoCamera();
            cam.Initialized += new EventHandler(cam_Initialized);
            cam.RecordingStarted += new EventHandler(cam_RecordingStarted);
            cam.RecordingStopped += new EventHandler<ContentReadyEventArgs>(cam_RecordingStopped);
            vCam.SetSource(cam);
        }

        //初始化LED
        void cam_Initialized(object sender, EventArgs e)
        {
            cam.LampEnabled = true;
            cam.StartRecording();
        }

        //LED关闭后
        void cam_RecordingStopped(object sender, ContentReadyEventArgs e)
        {
            IsShow = false;
            SwitchLight(false);
        }

        //LED开启后
        void cam_RecordingStarted(object sender, EventArgs e)
        {
            IsShow = true;
            SwitchLight(true);
        }

        //背景开关
        void SwitchLight(bool swit)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                string url = "icon/flashlight_level" + (swit ? "3" : "0") + ".png";
                Uri u = new Uri(url, UriKind.Relative);
                ContentPanel.Background = new ImageBrush() { ImageSource = new BitmapImage(u) };
            });
        }

        //开关按钮按下后松开ing
        private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (cam == null)
                {
                    StartCam();
                }
                else
                {
                    if (cam.IsRecording)
                    {
                        cam.StopRecording();
                        SwitchLight(false);
                    }
                    else
                    {
                        cam.StartRecording();
                        SwitchLight(true);
                    }
                }
            }
            catch (Exception)
            {

            }
            btn.Source = null;
        }

        //开关按钮按下ing
        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            btn.Source = new BitmapImage(new Uri("icon/close2_01.png", UriKind.Relative));
        }

        //离开页面时释放掉cam
        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            cam.Dispose();
            cam.Initialized -= cam_Initialized;
            cam.RecordingStarted -= cam_RecordingStarted;
            cam.RecordingStopped -= cam_RecordingStopped;
            cam = null;
        }

        //回退时关闭LED，有一些延迟是必须的。
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {

            if (IsShow)
            {
                cam.StopRecording();
                Thread.Sleep(400);
                SwitchLight(false);
            }
            base.OnBackKeyPress(e);
        }

    }
}
